package com.yeung.web;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class SpringServletContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
//        1. 创建ioc容器
        ServletContext servletContext = sce.getServletContext();
        String configLocation = servletContext.getInitParameter("spring_config");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configLocation);
//        2. 把ioc容器放在ServletContext的一个属性中
        servletContext.setAttribute("ApplicationContext", context);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
